import java.util.ArrayList;
import java.util.List;

/**
 * @USER: Administrator
 * @DATE: 2024/9/28 上午10:26
 * @VERSION: 1.0.0
 */
public class LC2899 {
    public static void main(String[] args) {

    }

    public List<Integer> lastVisitedIntegers(int[] nums) {
        List<Integer> seen = new ArrayList<>();
        List<Integer> ans = new ArrayList<>();
        int k =0;
        for (int num : nums) {
            if (num > 0) {
                k = 0;
                seen.add(0, num);
            } else {
                k++;
                if (k <= seen.size()) {
                    ans.add(seen.get(k - 1));
                } else {
                    ans.add(num);
                }
            }
        }
        return ans;
    }

}
